//author: create by SunJH
//时间: 2021/11/22 0022
//邮箱: 965989158@qq.com
//描述: 添加token的拦截器，该拦截器会根据请求类型不同，将token放到不同的地方
//TODO token获取和取消需要处理一下，需要改成自动获取token和删除本地token

import 'package:dio/dio.dart';

class TokenInterceptors extends InterceptorsWrapper {
  String _token = "";

  TokenInterceptors(this._token);

  @override
  void onRequest(RequestOptions options, RequestInterceptorHandler handler) {
    if (_token.isNotEmpty) {
      print(options.method);
    }
    switch (options.method) {
      case "POST":
        if (_token.isNotEmpty) options.data['token'] = _token;
        options.data['fromType'] = 'app';
        break;
      case "GET":
        if (_token.isNotEmpty) options.queryParameters['token'] = _token;
        options.queryParameters['fromType'] = 'app';
        break;
    }
    super.onRequest(options, handler);
  }

  void clearToken() => _token = "";
  set setToken(String token)=> _token = token;
}
